
# 1.分析远程图像

# 所需模块
import requests
%matplotlib inline
import matplotlib.pyplot as plt
import json
from PIL import Image
from io import BytesIO

# 输入终结点
endpoint = "https://ji.cognitiveservices.azure.com/"
# 输入密钥
subscription_key = "d6972f704cc5474b9a8b2e89fcb6b80e"

analyze_url = endpoint+ "vision/v2.1/analyze"

# 输入图片url
image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603521172323&di=6efa0e5fdac3171df251fc3a27f4adbf&imgtype=0&src=http%3A%2F%2Fimg.ewebweb.com%2Fuploads%2F20200102%2F23%2F1577980476-ZhPUJmYHki.jpg"
headers = {'Ocp-Apim-Subscription-Key': subscription_key}
# 参数
params = {'visualFeatures': 'Categories,Description,Color'}
# 请求主体bodyk
data = {'url': image_url}
response = requests.post(analyze_url, headers=headers,params=params, json=data)
response.raise_for_status()

analysis = response.json()
print(json.dumps(response.json()))
image_caption = analysis["description"]["captions"][0]["text"].capitalize()

# Display the image and overlay it with the caption.
image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)
plt.show()


# 2.分析本地照片


import os
import sys
import requests
import matplotlib.pyplot as plt      #调用所需要的模块
from PIL import Image
from io import BytesIO

analyze_url = "https://ji.cognitiveservices.azure.com//" + "vision/v3.1/analyze"
image_path = "/Users/79939/Desktop/API课程/timg.jpg"      # 分析的图片路径

image_data = open(image_path, "rb").read()
headers = {'Ocp-Apim-Subscription-Key':       "d6972f704cc5474b9a8b2e89fcb6b80e",      # 请求的头部信息
           'Content-Type': 'application/octet-stream'}

params = {'visualFeatures': 'Categories,Description,Color'}
response = requests.post(
    analyze_url, headers=headers, params=params, data=image_data)
response.raise_for_status()


analysis = response.json()
print(analysis)
image_caption = analysis["description"]["captions"][0]["text"].capitalize()


image = Image.open(BytesIO(image_data))
plt.imshow(image)
plt.axis("off")
_ = plt.title(image_caption, size="x-large", y=-0.1)

# 3.生成缩略图

import os
import sys
import requests
import matplotlib.pyplot as plt    # 调用所需模块
from PIL import Image
from io import BytesIO

thumbnail_url = "https://ji.cognitiveservices.azure.com/" + "vision/v2.1/generateThumbnail"  # 更换自己的终结点

image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603521172323&di=6efa0e5fdac3171df251fc3a27f4adbf&imgtype=0&src=http%3A%2F%2Fimg.ewebweb.com%2Fuploads%2F20200102%2F23%2F1577980476-ZhPUJmYHki.jpg"
# 插入图片url

headers = {'Ocp-Apim-Subscription-Key': "d6972f704cc5474b9a8b2e89fcb6b80e"}  # 请求头部
params = {'width': '100', 'height': '100', 'smartCropping': 'true'}          # 填入想要图片尺寸
data = {'url': image_url}
response = requests.post(thumbnail_url, headers=headers,
                         params=params, json=data)
response.raise_for_status()

thumbnail = Image.open(BytesIO(response.content))

plt.imshow(thumbnail)
plt.axis("off")

print("Thumbnail is {0}-by-{1}".format(*thumbnail.size))


# 4.地标

import os
import sys
import requests
%matplotlib inline
import matplotlib.pyplot as plt # 调用所需模块
from PIL import Image
from io import BytesIO

landmark_analyze_url = 'https://ji.cognitiveservices.azure.com/' + "vision/v3.1/models/landmarks/analyze" # 输入自己的终结点

image_url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603534750374&di=9d0c81563a08c440b2c0d2b5841535bf&imgtype=0&src=http%3A%2F%2Fpic.baike.soso.com%2Fp%2F20131220%2F20131220063939-1437565120.jpg" # 输入图片url
headers = {'Ocp-Apim-Subscription-Key': "d6972f704cc5474b9a8b2e89fcb6b80e"} #请求头部
params = {'model': 'landmarks'}
data = {'url': image_url}
response = requests.post(
    landmark_analyze_url, headers=headers, params=params, json=data)
response.raise_for_status()

analysis = response.json()
assert analysis["result"]["landmarks"] is not []
print(analysis)
landmark_name = analysis["result"]["landmarks"][0]["name"].capitalize()

image = Image.open(BytesIO(requests.get(image_url).content))
plt.imshow(image)
plt.axis("off")
_ = plt.title(landmark_name, size="x-large", y=-0.1)
plt.show()